<?php

// --------------------------------------------------------
// [ 实时采集 ]
// --------------------------------------------------------
// use app\common\lib\RealCollect as RealCollectCommonLib;
// $res = RealCollectCommonLib::run();
// if ($res['ret']) {
//     p($res['data']);
// } else {
//     // 采集失败
//     echo $res['msg'];
// }
// --------------------------------------------------------
// Array
// (
//     [ret] => 1
//     [data] => Array
//         (
//             [no] => 659686
//             [item] => Array
//                 (
//                     [0] => 02
//                     [1] => 07
//                     [2] => 01
//                     [3] => 09
//                     [4] => 04
//                     [5] => 10
//                     [6] => 08
//                     [7] => 06
//                     [8] => 03
//                     [9] => 05
//                 )
//         )
// )
// --------------------------------------------------------
namespace app\common\lib;

class RealCollect
{
    // 采集页面
    private static $_url = 'http://www.bwlc.gov.cn/';
    // 源数据
    private static $_sourceData;
    // 临时数据
    private static $_tempData;
    // 异常消息
    private static $_msg;
    // 返回结果
    private static $_result;

    public static function run()
    {
        // # 获取数据源
        // ------------------------------------------------------------------
        $bool = self::_getSourceData();
        if (!$bool) return ['ret'=>false,'msg'=>self::$_msg];
        // ------------------------------------------------------------------

        // # 解析期号
        // ------------------------------------------------------------------
        $bool = self::_analyNo();
        if (!$bool) return ['ret'=>false,'msg'=>self::$_msg];
        // ------------------------------------------------------------------

        // # 解析开奖结果
        // ------------------------------------------------------------------
        $bool = self::_analyResult();
        if (!$bool) return ['ret'=>false,'msg'=>self::$_msg];
        // ------------------------------------------------------------------

        return ['ret'=>true,'data'=>self::$_result];
    }

    // ----------------------------------------------------------------------

    // 获取源数据
    private static function _getSourceData()
    {
        self::$_sourceData = file_get_contents(self::$_url);
        if (!self::$_sourceData) {
            self::$_msg = '获取源数据失败';
            return false;
        }
        // echo self::$_sourceData;die;
        
        return true;
    }

    // 解析期号
    private static function _analyNo()
    {
        self::$_tempData = self::$_sourceData;
        $pattern = '/<div class="icon_pk10">(.*)<div class="pk10_bg">/iUs';
        $num = preg_match($pattern,self::$_tempData,$matches);
        if (0 == $num) {
            self::$_msg = '解析期号失败[1]';
            return false;
        }
        // print_r($matches[1]);die;

        self::$_tempData = $matches[1];
        $pattern = '/<span class="ml10 b red fa f14">(.*)<\/span>/iUs';
        $num = preg_match($pattern,self::$_tempData,$matches);
        if (0 == $num) {
            self::$_msg = '解析期号失败[2]';
            return false;
        }
        // print_r($matches[1]);die;
        
        self::$_result['no'] = $matches[1];
        // p(self::$_result);die;
        
        return true;
    }

    // 解析开奖结果
    private static function _analyResult()
    {
        self::$_tempData = self::$_sourceData;
        $pattern = '/<div class="pk10_bg">.*<ul class="dib">(.*)<\/ul>/iUs';
        $num = preg_match($pattern,self::$_tempData,$matches);
        if (0 == $num) {
            self::$_msg = '解析开奖结果失败[1]';
            return false;
        }
        // print_r($matches[1]);die;
        
        self::$_tempData = $matches[1];
        $pattern = '/<li>(.*)<\/li>/iUs';
        $num = preg_match_all($pattern,self::$_tempData,$matches);
        if (0 == $num) {
            self::$_msg = '解析开奖结果失败[2]';
            return false;
        }
        // print_r($matches[1]);die;
        
        self::$_result['item'] = $matches[1];
        self::$_result['time'] = self::_calcTime();
        // p(self::$_result);die;

        return true;
    }

    // 计算开奖时间
    private static function _calcTime()
    {
        $i[0] = date('i');
        $i[1] = substr($i[0],0,1);
        $i[2] = substr($i[0],1,1);
        // p($i);die;
        
        if ($i[2] >= 7) {
            $i[2] = 7;
        } elseif ($i[2] >= 2) {
            $i[2] = 2;
        } else {
            $i[2] = 7;
        }
        // echo $i[2];
        
        return strtotime( date('Y-m-d H:' . $i[1] . $i[2] . ':00') );
    }
}